package com.xuxiao.springbootexp01;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.GsonHttpMessageConverter;

import java.util.ArrayList;
import java.util.Collection;

@SpringBootApplication
public class SpringbootExp01Application implements CommandLineRunner {
    @Autowired
    ApplicationContext ac;

    public static void main(String[] args) {
        SpringApplication.run(SpringbootExp01Application.class, args);
    }

    @Bean
    public HttpMessageConverters httpMessageConverters(){
        Collection<HttpMessageConverter<?>> messageConverters=new ArrayList<>();
        GsonHttpMessageConverter gsonHttpMessageConverter=new GsonHttpMessageConverter();
        Gson gson=new GsonBuilder().setDateFormat("yyyy/MM/dd --- HH:mm:ss").create();
        gsonHttpMessageConverter.setGson(gson);
        messageConverters.add(gsonHttpMessageConverter);
        return new HttpMessageConverters(true,messageConverters);
    }

    @Override
    public void run(String... args) throws Exception {
        String[] beanName=ac.getBeanDefinitionNames();
        System.out.println("Spring IoC容器中注册的所有Bean:");
        for(String str:beanName){
            System.out.println("BeanName:"+str);
        }
        System.out.println("以上为Spring IoC容器中注册的所有Bean!");
    }
}
